home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swags_z.zip / STRINGS.SWG / 0096_RPos in BASM.pas < prev    next >
Pascal/Delphi Source File  |  1994-08-25  |  3KB  |  62 lines

  1. var
  2.   s1, s2: string;
  3.  
  4. function RPos( var str1, str2: string ): byte; assembler;
  5.   { returns position of the last occurrence of str1 in str2 }
  6.   { return value in AX }
  7.   { str1 - string to search for }
  8.   { str2 - string to search in  }
  9. asm
  10.         STD              { string operations backwards               }
  11.         LES   DI,Str2    { load in ES:DI pointer to str2             }
  12.         XOR   CH,CH      { clear CH                                  }
  13.         MOV   CL,[DI]    { length str2 --> CX                        }
  14.         AND   CX,CX      { length str2 = 0?                          }
  15.         JZ    @Negatief  { length str2 = 0, nothing to search in     }
  16.         ADD   DI,CX      { make DI point to the last char of str2    }
  17.         LDS   SI,Str1    { load in DS:SI pointer to str1             }
  18.         XOR   AH,AH      { clear AH                                  }
  19.         MOV   AL,[SI]    { load in AX length str1                    }
  20.         AND   AL,AL      { length str1 = 0?                          }
  21.         JZ    @Negatief  { length str1 = 0, nothing to search for    }
  22.         ADD   SI,AX      { make SI point to the last char of str1    }
  23.         MOV   AH,AL      { length str1 --> AH                        }
  24.         DEC   AH         { last char need not be compared again      }
  25.         LODSB            { load in AL last character of str1         }
  26. @Start:
  27.   REPNE SCASB            { scan for next occurrence 1st char in str2 }
  28.         JNE   @Negatief  { no success                                }
  29.         CMP   CL,AH      { length str1 > # chars left in str2 ?      }
  30.         JB    @Negatief  { yes, str1 not in str2                     }
  31.         MOV   DX,SI      { pointer to last but 1 char in str1 --> DX }
  32.         MOV   BX,CX      { number of chars in str2 to go --> BX      }
  33.         MOV   CL,AH      { length str1 --> CL                        }
  34.    REPE CMPSB            { compare until characters don't match      }
  35.         JE    @Positief  { full match                                }
  36.         SUB   SI,DX      {                                           }
  37.         NEG   SI         { prev. SI - current SI = # of chars moved  }
  38.         ADD   DI,SI      { reconstruct DI                            }
  39.         MOV   SI,DX      { restore pointer to 2nd char in str1       }
  40.         MOV   CX,BX      { number of chars in str2 to go --> BX      }
  41.         JMP   @Start     { scan for next occurrence 1st char in str2 }
  42. @Negatief:
  43.         XOR   AX,AX      { str1 is not in str, result 0              }
  44.         JMP   @Exit
  45. @Positief:
  46.         INC   BL
  47.         SUB   BL,AH      { start position of str1 in str2            }
  48.         MOV   AL,BL      { in AL                                     }
  49.         XOR   AH,AH      { clear AH                                  }
  50. @Exit:                   { we are finished. }
  51. end  { RPos };
  52.  
  53. begin
  54.   s1 := ParamStr( 1 );
  55.   s2 := ParamStr( 2 );
  56.   writeln( RPos( s1, s2 ) );
  57. end.
  58.  
  59. {
  60. If a '#' (shift-3) appears in the assembler source code, please replace
  61. that by a at-sign (shift-2).
  62. }